package com.ssy.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssy.bus.mapper.ProductionMapper;
import com.ssy.bus.pojo.Production;
import com.ssy.bus.service.IProductionService;
import com.ssy.bus.vo.ProductionVo;
import com.ssy.common.CodeMsg;
import com.ssy.common.Constant;
import com.ssy.common.utils.PageResult;
import com.ssy.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ssy
 */
@Service
public class ProductionServiceImpl extends ServiceImpl<ProductionMapper, Production> implements IProductionService {

    @Autowired
    private ProductionMapper productionMapper;

    @Override
    public PageResult loadAllProductionPage(ProductionVo productionVo) {
        Page<Production> page = new Page<>(productionVo.getPage(), productionVo.getLimit());
        IPage<Production> productionIPage = this.productionMapper.selectPage(page, new LambdaQueryWrapper<Production>()
                .like(StringUtils.hasText(productionVo.getName()), Production::getName, productionVo.getName())
                .like(StringUtils.hasText(productionVo.getKeyword()), Production::getKeyword, productionVo.getKeyword())
                .like(StringUtils.hasText(productionVo.getPhone()), Production::getPhone, productionVo.getPhone())
                .eq(productionVo.getState() != null, Production::getState, productionVo.getState())
                .ge(productionVo.getBeginTime() != null, Production::getCreateTime, productionVo.getBeginTime())
                .le(productionVo.getEndTime() != null, Production::getCreateTime, productionVo.getEndTime())
                .orderByDesc(Production::getCreateTime));
        return new PageResult(productionIPage.getTotal(), productionIPage.getRecords());
    }

    @Override
    public Result insertProduction(Production production) {
        if (production == null) {
            return new Result(CodeMsg.ADD_ERROR);
        }
        int i = this.productionMapper.insert(production);
        return i > 0 ? new Result(CodeMsg.ADD_SUCCESS) : new Result(CodeMsg.ADD_ERROR);
    }

    @Override
    public Result updateProduction(Production production) {
        if (production == null) {
            return new Result(CodeMsg.UPDATE_ERROR);
        }
        int i = this.productionMapper.updateById(production);
        return i > 0 ? new Result(CodeMsg.UPDATE_SUCCESS) : new Result(CodeMsg.UPDATE_ERROR);
    }

    @Override
    public Result deleteProductionById(Integer id) {
        if (id == null) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        int i = this.productionMapper.deleteById(id);
        return i > 0 ? new Result(CodeMsg.DELETE_SUCCESS) : new Result(CodeMsg.DELETE_ERROR);
    }

    @Override
    public Result bathDeleteProduction(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return new Result(CodeMsg.DELETE_ERROR);
        }
        ArrayList<Serializable> idList = new ArrayList<>();
        for (Integer id : ids) {
            idList.add(id);
        }
        this.productionMapper.deleteBatchIds(idList);
        return new Result(CodeMsg.DELETE_SUCCESS);
    }

    @Override
    public Result selectAllProduction() {
        List<Production> productions = this.productionMapper.selectList(new LambdaQueryWrapper<Production>()
                .select(Production::getId, Production::getName).eq(Production::getState, Constant.STATE));
        return new Result(productions);
    }

    @Override
    public Production selectOneById(Integer id) {
        return this.productionMapper.selectById(id);
    }
}
